from rest_framework.serializers import ModelSerializer
from .models import User, Cate, Shoes, Cart, Like, Score, Address, Order, OrderDeta, Type
from rest_framework import serializers
from drf_haystack.serializers import HaystackSerializer
from .search_indexes import ShoesIndex


class UserSer(ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'


class SerCate(ModelSerializer):
    class Meta:
        model = Cate
        fields = '__all__'


class SerShoes(ModelSerializer):
    ca = serializers.SerializerMethodField(read_only=True)

    def get_ca(self, obj):
        return obj.cate.name

    class Meta:
        model = Shoes
        fields = '__all__'


class SearchSer(HaystackSerializer):
    class Meta:
        index_classes = [ShoesIndex]
        fields = ['text', 'name', 'cate', 'price']


class CartSer(ModelSerializer):
    shoe = serializers.SerializerMethodField(read_only=True)

    def get_shoe(self, obj):
        return {'name': obj.shoes.name, 'price': obj.shoes.price, 'image': obj.shoes.image, 'desc': obj.shoes.desc}

    class Meta:
        model = Cart
        fields = '__all__'


class LikeSer(ModelSerializer):
    shoe = serializers.SerializerMethodField(read_only=True)

    def get_shoe(self, obj):
        return {'name': obj.shoes.name, 'price': obj.shoes.price, 'image': obj.shoes.image, 'desc': obj.shoes.desc}

    class Meta:
        model = Like
        fields = '__all__'


class ScoreSer(ModelSerializer):
    class Meta:
        model = Score
        fields = '__all__'


class AddressSer(ModelSerializer):
    class Meta:
        model = Address
        fields = '__all__'


class OrderSer(ModelSerializer):
    deta = serializers.SerializerMethodField(read_only=True)

    def get_deta(self, obj):
        return OrderDetaSer(obj.orderdeta_set.all(), many=True).data

    class Meta:
        model = Order
        fields = '__all__'


class OrderDetaSer(ModelSerializer):
    class Meta:
        model = OrderDeta
        fields = '__all__'


class TypeSer(ModelSerializer):
    class Meta:
        model = Type
        fields = '__all__'
